const articleService = require('../../service/admin/article')
const renameFile = require('../../utils/renameFile')
const fs = require('fs')
const path = require('path')
const articleController = {
    addArticle: async(req,res) => {
        // 处理文件路径重命名
        const cover = renameFile(req.file,req.file.mimetype.split("/")[1] )
        req.body.cover = cover
        // 自动添加当前登录管理员的 ID 作为作者
        req.body.authorID = req.adminUser.id
        const result = await articleService.addArticle(req.body)
        res.send({
            code: 200,
            msg: '添加成功',
            data: result
        })
    },
    getArticleList: async(req,res) => {
        const {page, pageSize,articleType} = req.query
        // 根据分页数据返回列表数据
        const left = (parseInt(page) - 1) * parseInt(pageSize)
        const right = left + parseInt(pageSize)
        // 获取当前登录用户ID
        const userID = req.adminUser.id
        const {data: result,total} = await articleService.getArticleList(left,right,parseInt(articleType),userID)

        res.send({
            code: 200,
            message: '获取分类列表成功',
            data: result,
            total: total,
        })
          
    },
    getArticleDetail: async(req,res) => {
        const {id} = req.query
        // 根据分类id获取分类详情
        const result = await articleService.getArticleDetail(id)
        res.send({
            code: 200,
            message: '获取文章详情成功',
            data: result
        })
    },
    updateArticle: async(req,res) => {
        if(req.file) {
            // 处理文件路径重命名
            const cover = renameFile(req.file,req.file.mimetype.split("/")[1] )
            req.body.cover = cover
            // 删除原来的文件
            const info = await articleService.getArticleDetail(req.body._id)
            const url = info.cover.replace(process.env.SERVER_BASE_URL,"")
            await fs.unlinkSync(path.join(__dirname,`../../public${url}`))
        } else {
            req.body.cover = req.body.cover.replace(process.env.SERVER_BASE_URL,"")
        }
        // 更新文章
        const result = await articleService.updateArticle(req.body)
        res.send({
            code: 200,
            message: '更新文章成功',
            data: req.body
        })
    },
    delArticle: async(req,res) => {
        const { id }  = req.params
        const result = await articleService.delArticle(id)
        res.send({
            code: 200,
            message: '删除文章成功',
        })
    },
    changePublish: async(req,res) => {
        const { id ,isPublish } = req.body
        const result = await articleService.changePublish(id,isPublish)
        res.send({
            code: 200,
            message: '发布状态切换成功',      
          })
    },
    searchArticle: async(req,res) => {
        const { page,pageSize, tagsID ,categoryID,articleType } = req.query
        const left = (parseInt(page) - 1) * parseInt(pageSize)
        const right = left + parseInt(pageSize)
        // 获取当前登录用户ID
        const userID = req.adminUser.id
        const result = await articleService.searchArticle(tagsID ,categoryID,parseInt(articleType),userID)
       console.log(result.length);
        let data = []
        if(result.length > 0) {
            result.forEach((item) => {
                for(let i  = 0; i < item.tags.length; i++) {
                    if(item.tags[i].id == tagsID) {
                        data.push(item);
                    }
                } 
                return false
            })
        }
        res.send({
            code: 200,
            message: '获取文章列表成功',
            data: data,
        })
    },
    delArticleRecover: async(req,res) => {
        const { id } = req.params
        const result = await articleService.delArticleRecover(id)
        res.send({
            code: 200,
            message: '恢复成功',   
        })
    },
    delArticlePermanently: async(req,res) => {
        const { id } = req.params
        const info = await articleService.getArticleDetail(id)
        // 删除文章图片;
        const url = info.cover.replace(process.env.SERVER_BASE_URL,"")
        await fs.unlinkSync(path.join(__dirname,`../../public${url}`))
        const result = await articleService.delArticlePermanently(id)

        res.send({
            code: 200,
            message: '彻底删除成功',   
        })
    },
    delArticleAll: async(req,res) => {
        const { ids } = req.body
        console.log(ids);
        // 删除文章图片;
        for(let item of ids) {
            // 删除文章图片;
            const info = await articleService.getArticleDetail(item)
            const url = info.cover.replace(process.env.SERVER_BASE_URL,"")
            await fs.unlinkSync(path.join(__dirname,`../../public${url}`))
        }
        const result = await articleService.delArticleAll(ids)
        res.send({
            code: 200,
            message: '批量删除成功',          
        })
    },
    statistics: async(req,res) => {
        // 获取当前登录用户ID
        const userID = req.adminUser.id
        const result = await articleService.statistics(userID)
        res.send({
            code: 200,
            message: '获取文章统计量成功',
            data: result,
        })
    }
}

module.exports = articleController